/**
 * @program: pettyloan
 * @ClassName RepayServiceImpl
 * @author: 汪江
 * @create: 2022-12-06 15:46
 **/
package com.wn.pettyloan.service.impl;

import com.wn.pettyloan.dto.RepayDto;
import com.wn.pettyloan.entity.Repay;
import com.wn.pettyloan.entity.RepayRecord;
import com.wn.pettyloan.mapper.RepayMapper;
import com.wn.pettyloan.mapper.RepayRecordMapper;
import com.wn.pettyloan.po.RepayPo;
import com.wn.pettyloan.service.ProductService;
import com.wn.pettyloan.service.RepayService;
import com.wn.pettyloan.util.Repayment;
import com.wn.pettyloan.util.utilentity.RePay;
import com.wn.pettyloan.util.utilentity.RepayPlan;
import com.wn.pettyloan.vo.RepayVo;
import org.apache.ibatis.annotations.Options;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class RepayServiceImpl implements RepayService {
    @Resource
    private RepayMapper repayMapper;
    @Resource
    private RepayRecordMapper repayRecordMapper;
    @Override
    //合同签订后，添加还款信息和第一个还款记录
    public boolean addRepay(Integer contractId) {
        //根据合同查
        RepayPo repayPo = repayMapper.selInfoByContractId(contractId);
        Date nowDate = new Date();
        //获取合同结束日期，三天内放款，本月不还
        GregorianCalendar calendar1 = new GregorianCalendar();
        calendar1.setTime(nowDate);
        calendar1.add(Calendar.DATE, 3);
        calendar1.add(Calendar.MONTH,repayPo.getRepayNum()+1);
        Date repayDeadline = calendar1.getTime();
        System.out.println("合同结束日期是"+repayDeadline);
        //设置还款信息参数
        Repay repay = new Repay();
        repay.setContractId(contractId);
        repay.setCurrentInterest(repayPo.getCurrentInterest());
        repay.setRepayShould(repayPo.getRepayShoule());
        repay.setRepayAlready(new BigDecimal(0));
        repay.setRepayDeadline(repayDeadline);
//        拆入还款表
        int i = insertSelective(repay);
        System.out.println("主键是"+repay.getRepayId());
        //三天内放款，本月不还，下个月第一期
        Calendar calendar2 = new GregorianCalendar();
        calendar2.setTime(nowDate);
        calendar2.add(Calendar.DATE, 3);
        calendar2.add(Calendar.MONTH,2);
        Date overdueTime = calendar2.getTime();//第一次还款日期
        System.out.println("第一次还款日期是"+overdueTime);
        //计算下一期应还本金，利息，总额
        RepayPlan repayPlan = new RepayPlan();
        repayPlan.setPrincipal(repayPo.getRepayShoule().doubleValue());
        repayPlan.setInterestRate(repayPo.getCurrentInterest().doubleValue());
        repayPlan.setSumMonth(repayPo.getRepayNum());
        repayPlan.setNowMonth(1);
        repayPlan.setAlreadyRepy(0);
        System.out.println(repayPlan.toString());
        System.out.println(repayPo);
        RePay rePay = new RePay();
        String s = repayPo.getRepayMethodName();
        if (s.equals("等本等息")) {
            rePay = Repayment.method1(repayPlan);
        } else if (s.equals("等额本息")) {
            rePay = Repayment.method2(repayPlan);
        } else if (s.equals("等额本金")) {
            rePay = Repayment.method3(repayPlan);
        } else if (s.equals("先息后本")) {
            rePay = Repayment.method4(repayPlan);
        }
        System.out.println(rePay.toString());
        //设置下次一次还款记录
        RepayRecord repayRecord = new RepayRecord();
        repayRecord.setRepayId(repay.getRepayId());
        repayRecord.setRepayCurrentPeriod(1);
        repayRecord.setRepayNum(repayPo.getRepayNum());
        repayRecord.setCurrentPrincipalPayable(new BigDecimal(rePay.getPrincipal()));
        repayRecord.setCurrentInterestPayable(new BigDecimal(rePay.getInterest()));
        repayRecord.setCurrentTotalMoney(new BigDecimal(rePay.getTotal()));
        repayRecord.setOverdueTime(overdueTime);
        repayRecord.setRepayRecordStatus(2);
        int i1 = repayRecordMapper.insertSelective(repayRecord);
        if(i1>0){
            return true;
        }else {
            return false;
        }
    }


    @Override
    public Repay selRepayAlready(Integer repayRecordId) {
        return repayMapper.selRepayAlready(repayRecordId);
    }

    @Override
    public RepayVo selTopInfo(RepayDto repayDto) {
        return repayMapper.selTopInfo(repayDto);
    }

    @Override
    public List<RepayVo> selAllRepay(RepayDto repayDto) {
        return repayMapper.selAllRepay(repayDto);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return repayMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Repay record) {
        return repayMapper.insert(record);
    }

    @Override
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    public int insertSelective(Repay record) {
        return repayMapper.insertSelective(record);
    }

    @Override
    public Repay selectByPrimaryKey(Long id) {
        return repayMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Repay record) {
        return repayMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Repay record) {
        return repayMapper.updateByPrimaryKey(record);
    }
}
